import isPrime from "./isPrime"
import getRandomColor, { getRandomNum } from "./randomColor"

export default class NumberTimer {
    constructor(duration = 500, number = 0) {
        this.duration = duration;
        this.number = number;
        this.timer = null;
        this.divContainer = document.getElementById("divContainer");
        this.divCenter = document.getElementById("divCenter");
    }

    render() {  
        const span = document.createElement("span");
        span.innerText = this.number;

        if(isPrime(this.number)){
            let randomColor = getRandomColor();
            span.style.color = randomColor;
            const div = document.createElement('div');
            div.style.color = randomColor;
            div.className = "center"
            div.innerText = this.number;
            document.body.appendChild(div);
            window.getComputedStyle(div, null).height;
            div.style.transform = `translate(${getRandomNum(-200, 200)}px, ${getRandomNum(-200, 200)}px)`
            div.style.opacity = '0'
        }
        this.divContainer.appendChild(span);
        this.divCenter.innerText = this.number;


    }

    start(){
        if(this.timer) {
            return;
        }
        this.timer = setInterval(() => {
            this.render();
            this.number ++;  
        }, this.duration);
    }

    stop() {
        clearInterval(this.timer);
        this.timer = null;
    }

}